/*
 * ============================================================================
 * Copyright Notice!
 * ----------------------------------------------------------------------------
 * This file contains proprietary and confidential information belonging to the
 * group of people involvled in developing this project.  Reproduction, disclosure 
 * or use without specific written authorization from project stake holders is 
 * prohibited. Please refer the readme.txt in project installation folder for more
 * specific contacts
 *
 * All rights reserved.
 * ============================================================================
 */
package edu.uwm.universitydb.dao.student;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import edu.uwm.universitydb.encryption.JavaEncryption;
import edu.uwm.universitydb.encryption.MissingKeyException;
import edu.uwm.universitydb.framework.nontransactional.db.command.impl.NonTransactionalDataManipulationCommandBase;
import edu.uwm.universitydb.model.Department;
import edu.uwm.universitydb.model.Student;

/**
 * <code>UpdateStudentCommand</type>
 * command class to update  student table
 * 
 */
public class UpdateStudentCommand extends NonTransactionalDataManipulationCommandBase
{
    /** static string holding the update sql */
    private static final String SQL =
        "update university.grad_student set name=?, age=?, program=?, dno=? " +
        "where grad_ssn=?";

    /** attribute holding the student to be updated */
    private final Student mStudent;

    /**
     * 
     * @param student
     */
    public UpdateStudentCommand(final Student student)
    {
        mStudent = student;
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.framework.db.command.DBCommand#getSQL()
     */
    @Override
    public String getSQL()
    {
        return SQL;
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.framework.db.command.DBCommand#setParameters(java.sql.PreparedStatement)
     */
    @Override
    public void setParameters(final PreparedStatement pStmt) throws SQLException
    {

        // set name
        final String name = mStudent.getName();
        pStmt.setString(1, name);

        // set age
        String age = mStudent.getStrAge();
		try {
	        JavaEncryption jce = JavaEncryption.getInstance();
			age = jce.encrypt(age);
		} catch (MissingKeyException e) {
			//set empty age
			age = "";
		}
        pStmt.setString(2, age);

        // set program
        final String program = mStudent.getProgram();
        pStmt.setString(3, program);

        // set dept num
        final Department dept = mStudent.getDepartment();
        final Integer deptNum = dept.getDeptId();
        pStmt.setInt(4, deptNum);
        
        // set ssn
        final String ssn = mStudent.getSsnNum();
        pStmt.setString(5, ssn);

    }

}
